package cn.example.demo.controller;

import cn.example.demo.exception.ResultCode;
import cn.example.demo.service.ArticleService;
import cn.example.demo.vo.ArticleVo;
import cn.example.demo.vo.ResultVO;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;

/**
 * @ProjectName: springbootmybatisinvoicings
 * @Package: cn.example.demo.controller
 * @ClassName: UploadFile
 * @Author: liang
 * @Description:
 * @Date: 2020/4/9 14:55
 * @Version: 1.0
 */
@RestController
@RequestMapping("test")
public class ArticlContorller {
    @Autowired
    private ArticleService articleService;

    @RequestMapping("pageinate")
    public ResultVO pageinate(@RequestBody JSONObject jsonStr) {
        String pageNumber = jsonStr.getString("pageNumber");
        String pageSize = jsonStr.getString("pageSize");
        return new ResultVO<>(ResultCode.SUCCESS, articleService.pageinate(Integer.parseInt(pageNumber), Integer.parseInt(pageSize)));
    }
    @RequestMapping("pageinate1")
    public ResultVO pageinate1(@RequestBody JSONObject jsonStr) {
        String pageNumber = jsonStr.getString("pageNumber");
        String pageSize = jsonStr.getString("pageSize");
        String test = jsonStr.getString("test");
        System.out.println(jsonStr);
        return new ResultVO<>(ResultCode.SUCCESS, articleService.pageinate(Integer.parseInt(pageNumber), Integer.parseInt(pageSize),test,null));
    }

    @RequestMapping("selectArticle")
    public Object selectArticle() {
        return articleService.select();
    }

    @RequestMapping("selectone")
    public Object selectone() {
        return articleService.findById(2).toRecord().getColumns();
    }

    @RequestMapping("delete")
    public Object delete(Integer id) {
        // String ids = req.getParameter("id");
        System.out.println(id);
        //System.out.println(ids);
        return articleService.delete(id);
    }

    @RequestMapping("update")
    public ResultVO update(@RequestBody @Valid ArticleVo articleVo) {
        if (articleService.update(articleVo.getId(), articleVo.getSave(), articleVo.getTest())) {
            return new ResultVO<>(ResultCode.SUCCESS);
        } else {
            return new ResultVO<>(ResultCode.FAILED);
        }
    }

    @RequestMapping("insert")
    public ResultVO save(@RequestBody @Valid ArticleVo articleVo) {
        if (articleService.insert(articleVo)) {
            return new ResultVO<>(ResultCode.SUCCESS);
        } else {
            return new ResultVO<>(ResultCode.FAILED);
        }
    }
}
